package gui.buttons;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
import events.ResizeEvent;

/**
 * ...
 * @author Damien Mabin
 */

class SimpleButton extends Sprite
{

	// Taille du bord en pixel
	private var m_TextFormat 		: TextFormat;
	public  var m_Label 			: TextField;
	private var m_OverFG 			: Bitmap;
	private var m_Style				: { backgroundColor : UInt, backgroundAlpha : Float, borderColor : UInt, borderAlpha : Float, borderSize : Int, foregroundColor : UInt };
	
	public function new(f_Label : String, f_TextFormat : TextFormat = null, ?f_Style : { backgroundColor : UInt, backgroundAlpha : Float, borderColor : UInt, borderAlpha : Float, borderSize : Int, foregroundColor : UInt}) 
	{
		super();
		m_Style = ((f_Style == null) ? { backgroundColor : 0x000000, backgroundAlpha : 1.0, borderColor : 0x0000FF, borderAlpha : 1.0, borderSize : 0, foregroundColor : 0x88FFFFFF } : f_Style);
		
		// Initialisation de la couleur de fond
		this.graphics.clear();
		this.graphics.beginFill(m_Style.backgroundColor, m_Style.backgroundAlpha);
		if(m_Style.borderSize > 0)
			this.graphics.lineStyle(m_Style.borderSize, m_Style.borderColor, m_Style.borderAlpha);
		this.graphics.drawRect(0, 0, 10, 10);
		
		// Zone affichée quand on a la souris sur le TreeItem
		m_OverFG = new Bitmap(new BitmapData(10, 10, true, m_Style.foregroundColor));
		m_OverFG.x = 0; 
		m_OverFG.y = 0;
		m_OverFG.visible = false;
		this.addChild(m_OverFG);
		
		m_Label = new TextField();
		m_Label.selectable = false;
		m_Label.autoSize = TextFieldAutoSize.LEFT;
		m_Label.text = f_Label;
		if(f_TextFormat != null)
			m_Label.setTextFormat(f_TextFormat);
		this.addChild(m_Label);
		
		this.addEventListener(ResizeEvent.EVENT, OnResize);
		this.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
		this.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
	}
	
	public function OnResize(f_Evt : ResizeEvent) : Void
	{
		m_Label.x = 0;
		m_Label.y = 0;
		m_Label.width = f_Evt.m_Width;
		m_Label.height = f_Evt.m_Height;
		
		// Zone affichée quand on a la souris sur le TreeItem
		m_OverFG = new Bitmap(new BitmapData(Std.int(f_Evt.m_Width), Std.int(f_Evt.m_Height), true, m_Style.foregroundColor));
		m_OverFG.x = 0; 
		m_OverFG.y = 0;
		m_OverFG.visible = false;
		
		this.graphics.clear();
		this.graphics.beginFill(m_Style.backgroundColor, m_Style.backgroundAlpha);
		if(m_Style.borderSize > 0)
			this.graphics.lineStyle(m_Style.borderSize, m_Style.borderColor, m_Style.borderAlpha);
		this.graphics.drawRect(0, 0, f_Evt.m_Width, f_Evt.m_Height);
		
		width = f_Evt.m_Width;
		height = f_Evt.m_Height;
    }
	
	private function mouseOverHandler(f_ME : MouseEvent) : Void
	{
		m_OverFG.visible = true;
    }
	
	private function mouseOutHandler(f_ME : MouseEvent) : Void
	{
		m_OverFG.visible = false;
    }
}